<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/7
 * Time: 18:30
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class ZhuanxianController extends CommonController{
    //专线管理查询
    public function index(){
        $count = M("weig_add")->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '尾页');
        $show       = $Page->show();// 分页显示输出
        //查询数据,多表关联
        $data = M("weig_add")->alias("w")
            ->join("tb_starting s on s.id=w.start_id")
            ->join("tb_partition_country pc on pc.id=s.pcid")
            ->join("tb_channel_partition cp on cp.id=s.cpid")
            ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
            ->join("tb_channel q on q.id=cp.channel_id")
            ->join("tb_country c on c.id=pc.country_id")
            ->field("p.partition,w.id,c.ename,c.cname,q.company,w.start_weight,w.start_fare,w.stage_min_weig,w.stage_max_weig,w.add_weig,w.add_weig_price,q.fuel")
            ->order("q.company,p.partition,c.ename,w.stage_min_weig,w.add_time desc")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign("page",$show);//把分页数据赋给模板
        $this->assign("data",$data);//把数据赋给模板
        $country = M("country")->where('pid=0')->order("ename")->select();//查国家数据
        $channel = M("channel")->select();//查渠道数据
        $partition = M("partition")->select();
        $this->assign("partition",$partition);
        $this->assign("country",$country);
        $this->assign("channel",$channel);
        $this->display();
    }
    //筛选
    public function search(){
        $get = I("get.");
        $country= "c.id=".$get['country_id'];
        $channel ="q.id=". $get['channel_id'];
        $partition = "p.id=". $get['partition_id'];

        if($get['country_id']&&$get['channel_id']&&$get['partition_id']){
            $str=$country." and ".$channel." and ".$partition;
        }elseif($get['country_id']&&$get['channel_id']){
            $str=$country." and ".$channel;
        }elseif($get['channel_id']&&$get['partition_id']){
            $str=$channel." and ".$partition;
        }elseif($get['country_id']&&$get['partition_id']){
            $str=$country." and ".$partition;
        }elseif($get['country_id']){
            $str=$country;
        }elseif($get['channel_id']){
            $str=$channel;
        }elseif($get['partition_id']){
            $str=$partition;
        }else{
            $str='';
        }
            $data =M("weig_add")->alias("w")
                ->join("tb_starting s on s.id=w.start_id")
                ->join("tb_partition_country pc on pc.id=s.pcid")
                ->join("tb_channel_partition cp on cp.id=s.cpid")
                ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                ->join("tb_channel q on q.id=cp.channel_id")
                ->join("tb_country c on c.id=pc.country_id")
                ->field("p.partition,w.id,c.ename,c.cname,q.company,w.start_weight,w.start_fare,w.stage_min_weig,w.stage_max_weig,w.add_weig,w.add_weig_price,q.fuel")
                ->order("q.company,c.ename,w.stage_min_weig desc")
                ->where($str)
                ->select();

        if($data){
            echo json_encode($data);
        }else{
            echo 0;
        }

    }
    //添加的ajax操作
    public function add_check_ajax(){
        if(empty($_GET['country_id'])){
            $where="cp.channel_id=".$_GET['channel_id']." and p.id=".$_GET['partition_id'];
        }elseif(empty($_GET['channel_id'])){
            $where="pc.country_id=".$_GET['country_id']." and p.id=".$_GET['partition_id'];
        }elseif(empty($_GET['partition_id'])){
            $where="pc.country_id=".$_GET['country_id']." and cp.channel_id=".$_GET['channel_id'];
        }else{
            $where="pc.country_id=".$_GET['country_id']." and cp.channel_id=".$_GET['channel_id']." and p.id=".$_GET['partition_id'];
        }
        $start =M("starting")->alias("s")
            ->join("tb_partition_country pc on pc.id=s.pcid")
            ->join("tb_channel_partition cp on cp.id=s.cpid")
            ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
            ->join("tb_channel q on q.id=cp.channel_id")
            ->join("tb_country c on c.id=pc.country_id")
            ->where($where)->select();
        if($start){
            echo json_encode($start[0]);
        }else{
            echo 0;
        }
    }
    //根据渠道拿分区
    public function cp(){
        $part = M("channel_partition")->alias("cp")->join("tb_partition p on p.id=cp.partition_id")->where("cp.channel_id={$_GET['channel_id']}")->group("p.partition")->select();
        if($part){
            echo json_encode($part);
        }else{
            echo 0;
        }
    }
    public function qc(){
        $count =M("country")->group("cname")->order("`order` desc")->select();
        if($count){
            echo json_encode($count);
        }else{
            echo 0;
        }
    }
    //根据分区拿国家
    public function pc(){
        if($_GET['partition_id']==1){
            $parcou =M("country")->alias("c")->field("id,cname,ename")->where("pid=0")->select();
        }else{
            $parcou = M("partition_country")->alias("pc")->join("tb_country c on c.id=pc.country_id")->where("pc.partition_id={$_GET['partition_id']}")->group("c.id")->select();
        }
        if($parcou){
            echo json_encode($parcou);
        }
    }
    //添加时渠道改变的ajax操作
    public function channel_change(){
        $start =M("starting")->alias("s")
            ->join("tb_channel_partition cp on cp.id=s.cpid")
            ->join("tb_partition_country pc on pc.id=s.pcid")
            ->join("tb_channel q on q.id = cp.channel_id")
            ->where("q.id={$_GET['channel_id']}")->select();
        if($start){
            echo json_encode($start[0]);
        }else{
            echo 0;
        }
    }
    //添加
    public function add(){
        if(!IS_POST){
            $country = M("country")->where("pid=0")->order("ename")->select();//国家数据
            $channel = M("channel")->order("company")->select();//渠道数据
            $partition= M("partition")->order("partition")->select();//分区数据
            $this->assign("partition",$partition);
            $this->assign("channel",$channel);
            $this->assign("country",$country);
            $this->display();
        }else{
            $post =I("post.");
            $post['add_time']=date("Y-m-d H:i:s",time());
            $post["start_fare"]=array_filter ($post["start_fare"]);
            $post['stage_min_weig']=array_filter($post['stage_min_weig']);
            $post['stage_max_weig']=array_filter($post['stage_max_weig']);
            foreach($post["start_fare"] as $k=>$v){
                if($post["type"]==1){
                $arr[]=["start_fare"=>$v,"stage_min_weig"=>$post['stage_min_weig'][$k],"stage_max_weig"=>$post['stage_max_weig'][$k]];
                }else{
                    $arr[]=["start_fare"=>$v,"stage_min_weig"=>$post['stage_min_weig'][$k],"stage_max_weig"=>$post['stage_max_weig'][$k],"start_weight"=>$post['stage_min_weig'][$k]];
                }
            }
            $start = M("starting")->alias("s")
                ->join("tb_weig_add w on w.start_id=s.id")
                ->join("tb_channel_partition cp on cp.id=s.cpid")
                ->join("tb_partition_country pc on pc.id=s.pcid")
                ->field("s.*")
                ->where("cp.channel_id={$post['channel_id']} and pc.partition_id={$post['partition_id']} and pc.country_id={$post['country_id']} and w.type={$post['type']}")->select();//查数据是否存在

            if(!$start){//数据不存在
                //判断是否有无分区
                /*if($post['partition_id']==1){//无分区
                     $post['pcid']=0;
                }else{*/

                    $post["pcid"]=M("partition_country")->data($post)->add();//添加数据并拿到插入数据id
                //}
                $post['cpid']=M("channel_partition")->data($post)->add();//添加数据并拿到插入数据id
                $post['start_id'] =M("starting")->data($post)->add();//添加数据并拿到插入数据id
            }else{
                M("starting")->where("id={$start[0]['id']}")->data($post)->save();
                $post['start_id']=$start[0]['id'];//数据存在直接拿id
            }
            if($post["type"]==1){
                $post["stage_min_weig"]=$arr[0]["stage_min_weig"];
                $post["stage_max_weig"]=$arr[0]["stage_max_weig"];
                $post["start_fare"]=$arr[0]["start_fare"];
                if($post['start_weight']>1){
                    $post['start_fare']=$post['start_weight']*$post['add_weig_price'];
                }
                M("channel")->where("id={$post['channel_id']}")->save($post);
                $wadd=M("weig_add")
                    ->field("start_id,stage_min_weig,stage_max_weig,add_weig,add_weig_price,start_weight,start_fare,type")
                    ->where("start_id={$post['start_id']} and start_weight={$post['start_weight']}")->select();
                if($wadd==false){
                    $bol =M("weig_add")->data($post)->add();//插入数据
                    if($bol){
                        $this->success("添加成功",__MODULE__."/Zhuanxian/index",0);
                    }else{
                        $this->error("添加失败",__MODULE__."/zhuanxian/add",0);
                    }
                }else{
                    $this->error("添加失败!你添加的数据已存在");
                }
            }else{
                M("channel")->where("id={$post['channel_id']}")->save($post);//燃油
                foreach($arr as $val){
                    $wadd=M("weig_add")
                        ->field("start_id,stage_min_weig,stage_max_weig,add_weig,add_weig_price,start_weight,start_fare,type")
                        ->where("start_id={$post['start_id']} and start_weight={$val['start_weight']}")->select();
                    if($wadd==false){
                        $post["start_weight"]=$val["start_weight"];
                        $post["start_fare"]=$val["start_fare"];
                        $post["stage_min_weig"]=$val["stage_min_weig"];
                        $post['stage_max_weig']=$val['stage_max_weig'];
                        $bol =M("weig_add")->data($post)->add();//插入数据
                    }else{
                        continue;
                    }
                }
                    if($bol){
                        $this->success("添加成功",__MODULE__."/Zhuanxian/index",0);
                    }else{
                        $this->error("添加失败",__MODULE__."/zhuanxian/add",0);
                    }

               /* $post['stage_max_weig']=$post['stage_min_weig']=$post['start_weight'];*/
            }

        }
    }
    //修改
    public function edit(){
        if(IS_POST) {
            $id=$_GET['nid'];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/zhuanxian/index');
            }else{
                $post=I("post.");
                $data=M("weig_add")->alias("w")
                    ->join("tb_starting s on s.id=w.start_id")
                    ->join("tb_partition_country pc on pc.id=s.pcid")
                    ->join("tb_channel_partition cp on cp.id=s.cpid")
                    ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                    ->join("tb_channel q on q.id=cp.channel_id")
                    ->join("tb_country c on c.id=pc.country_id")
                    ->field("pc.country_id,cp.channel_id,s.announcements,s.method,cp.partition_id,q.fuel,w.stage_min_weig,w.stage_max_weig,w.add_weig,w.add_weig_price,w.start_weight,w.start_fare,w.type")
                    ->where("w.id={$id}")
                    ->select();
                if(array_diff_assoc($data[0],$post)){
                    /*$bol=M("weig_add")->alias("w")
                        ->join("tb_starting s on s.id=w.start_id")
                        ->join("tb_partition_country pc on pc.id=s.pcid")
                        ->join("tb_channel_partition cp on cp.id=s.cpid")
                        ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                        ->data($post)
                        ->where("w.id={$id}")->save();
                    echo M("weig_add")->getlastsql();
                    exit;
                    if($bol) {
                        $this->success('操作成功！',__MODULE__.'/zhuanxian/index');
                    } else {
                        $this->error("操作失败");
                    }*/
                    $data =M("weig_add")->alias("w")->field("w.start_id")->where("w.id={$id}")->select();
                    $wa = M("weig_add")->field("stage_min_weig,stage_max_weig,add_weig,add_weig_price,start_weight,start_fare,type")->where("id={$id}")->select();
                    $start = M("starting")->alias("s")
                        ->join("tb_partition_country pc on pc.id=s.pcid")
                        ->join("tb_channel_partition cp on cp.id=s.cpid")
                        ->field("s.announcements,s.method")
                        ->where("s.id={$data[0]['start_id']}")->select();
                    $st=M("starting")->alias("s")->where("s.id={$data[0]['start_id']}")->select();
                    $cpdata =M("channel_partition")->field("channel_id,partition_id")->where("id={$st[0]['cpid']}")->select();
                    $pcdata =M("partition_country")->field("country_id,partition_id")->where("id={$st[0]['pcid']}")->select();
                    $fuel=M("channel")->where("id={$cpdata[0]['channel_id']}")->select();
                    if($post["fuel"]!=$fuel[0]["fuel"]){
                        $cha=M("channel")->where("id={$cpdata[0]['channel_id']}")->save($post);
                    }
                    if($post["type"]==1){
                        if($post['start_weight']>1){
                            $post['start_fare']=$post['start_weight']*$post['add_weig_price'];
                        }
                    }else{
                        /*$post['stage_max_weig']=$post['stage_min_weig']=$post['start_weight'];*/
                    }
                    if(array_diff_assoc($wa[0],$post)){
                        $test = M("weig_add")->where("id={$id}")->save($post);
                    }
                    if(array_diff_assoc($start[0],$post)){
                        $bol = M("starting")->where("id={$data[0]['start_id']}")->save($post);
                    }
                    if(array_diff_assoc($cpdata[0],$post)){
                        $cp = M("channel_partition")->where("id={$st[0]['cpid']}")->save($post);
                    }
                    if(array_diff_assoc($pcdata[0],$post)){
                        $pc = M("partition_country")->where("id={$st[0]['pcid']}")->save($post);
                    }
                    if($test||$bol||$pc||$cp||$cha) {
                        $this->success('操作成功！',__MODULE__.'/zhuanxian/index');
                    } else {
                        $this->error("操作失败");
                    }
                }else{
                    $this->error("你没有更新的内容");
                }
            }
        }else{
            $id=$_GET["nid"];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/zhuanxian/index');
            }else{
                $arr=M("weig_add")->alias("w")
                    ->join("tb_starting s on s.id=w.start_id")
                    ->join("tb_partition_country pc on pc.id=s.pcid")
                    ->join("tb_channel_partition cp on cp.id=s.cpid")
                    ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                    ->join("tb_channel q on q.id=cp.channel_id")
                    ->join("tb_country c on c.id=pc.country_id")
                    ->where("w.id={$id}")->select();
                $country = M("country")->where("pid=0")->order("ename")->select();
                $channel = M("channel")->order("company")->select();
                $partition= M("partition")->order("partition")->select();//分区数据
                $this->assign("partition",$partition);
                $this->assign("country",$country);
                $this->assign("channel",$channel);
                $this->assign("arr",$arr);
                $this->display();
            }
        }
    }
    //删除
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/zhuanxian/index');
        }
        if(M("weig_add")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/zhuanxian/index',1);
        } else {
            $this->error('操作失败！',__MODULE__.'/zhuanxian/index');
        }
    }
    //空操作
    public function _empty(){
        $this->error("404");
    }
}